package guda.house.web.action.api;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.ApiFeeBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.query.ApiFeeQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by well on 16/3/4.
 */
@Controller
@RequestMapping(value = "api/fee")
public class ApiFeeAction {

    @Autowired
    private ApiFeeBiz apiFeeBiz;

    @RequestMapping(value = "queryPayLog.htm")
    public void queryPayLog(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        String outUserId = request.getParameter("outUserId");
        int pageNo = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        JsonResult result = JsonResult.getInstance();
        if (StringUtils.isEmpty(outUserId)) {
            result.setErrMsg("缺少参数!");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        ApiFeeQuery apiFeeQuery = new ApiFeeQuery();
        apiFeeQuery.setStartDate(startDate);
        apiFeeQuery.setEndDate(endDate);
        apiFeeQuery.setPageSize(pageSize);
        apiFeeQuery.setPageNo(pageNo);
        apiFeeQuery.setOutUserId(outUserId);
        BizResult bizResult = apiFeeBiz.queryPaiedFee(apiFeeQuery);

        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }

    @RequestMapping(value = "queryUnPayList.htm")
    public void queryUnPayList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        String outUserId = request.getParameter("outUserId");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        JsonResult result = JsonResult.getInstance();
        if (StringUtils.isEmpty(outUserId)) {
            result.setErrMsg("缺少参数!");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        ApiFeeQuery apiFeeQuery = new ApiFeeQuery();
        apiFeeQuery.setOutUserId(outUserId);
        apiFeeQuery.setStartDate(startDate);
        apiFeeQuery.setEndDate(endDate);
        BizResult bizResult = apiFeeBiz.queryUnpayFee(apiFeeQuery);

        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }

    @RequestMapping(value = "doPay.htm")
    public void doPay(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        String outUserId = request.getParameter("outUserId");
        String billIdList = request.getParameter("billIdList");
        JsonResult result = JsonResult.getInstance();
        if (StringUtils.isEmpty(outUserId)||StringUtils.isEmpty(billIdList)) {
            result.setErrMsg("缺少参数!");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

        BizResult bizResult = apiFeeBiz.pay(outUserId,billIdList);

        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
